package top.itmeans.yes.admin.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.itmeans.yes.common.api.CommonResult;
import top.itmeans.yes.api.CommonPage;
import top.itmeans.yes.dto.OrderReturnApplyResult;
import top.itmeans.yes.dto.ReturnApplyQueryParam;
import top.itmeans.yes.dto.UpdateStatusParam;
import top.itmeans.yes.entity.OrderReturnApply;
import top.itmeans.yes.service.OrderReturnApplyService;

import java.util.List;

/**
 * <p>
 * 订单退货申请 前端控制器 OMS
 * </p>
 *
 * @author Yiming.Cao
 * @since 2022-04-28
 */
@RestController
@RequestMapping("/returnApply")
public class OrderReturnApplyController {
    @Autowired
    private OrderReturnApplyService returnApplyService;

    /**
     * GET [host:ip/serverPath]/orderReturnApply/list?pageSize=xxx&pageNum=xxx
     * @param queryParam
     */
    @ApiOperation("分页查询退货申请")
    @GetMapping(value = "/list")
    public CommonResult<CommonPage<OrderReturnApply>> list(ReturnApplyQueryParam queryParam,
                                                           @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                           @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
        Page<OrderReturnApply> returnApplyPage = returnApplyService.list(queryParam, pageSize, pageNum);
        return CommonResult.success(CommonPage.restPage(returnApplyPage));
    }

    /**
     * DELETE [host:ip/serverPath]/orderReturnApply?ids=xxx
     */
    @ApiOperation("批量删除退货申请")
    @DeleteMapping
    public CommonResult delete(@RequestParam("ids") List<Long> ids) {
        int count = returnApplyService.delete(ids);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }

    /**
     * GET [host:ip/serverPath]/orderReturnApply/{id}
     */
    @ApiOperation("获取退货申请详情")
    @GetMapping(value = "/{id}")
    public CommonResult getItem(@PathVariable Long id) {
        OrderReturnApplyResult result = returnApplyService.getItem(id);
        return CommonResult.success(result);
    }

    /**
     * POST [host:ip/serverPath]/orderReturnApply/status/{id}
     */
    @ApiOperation("修改退货申请状态")
    @PostMapping(value = "/status/{id}")
    public CommonResult updateStatus(@PathVariable Long id, @RequestBody UpdateStatusParam statusParam) {
        int count = returnApplyService.updateStatus(id, statusParam);
        if (count > 0) {
            return CommonResult.success(count);
        }
        return CommonResult.failed();
    }
}
